from flask_restx import fields

RESULT_SCHEME = {
    'code': fields.Integer(default=200),
    'msg': fields.String(),
    'errors': fields.String(),
}


class DateFormat(fields.Raw):
    def __init__(self, *args, **keyword):
        super().__init__(*args, **keyword)

    def format(self, value):
        return value.strftime('%Y-%m-%d %H:%M:%S')


def response(data, code=200, msg=None, errors=''):
    # The list of errors is allowed to be supplied manually, 
    # but must eventually generate a string that is converted to a py object in the response middleware
    if isinstance(errors, list):
        errors = str(errors)
    return {
        'data': data,
        'code': code,
        'msg': msg,
        'errors': errors, 
    }
